﻿// M01();
using System.Collections;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json.Serialization;
using System.Xml.Linq;
using Newtonsoft.Json;

// M02();
// M03();
// M04();
// M05();
// M06();
// M07();
// M08();
// M09();
M10();

static void M01()
{
    // var p = new Person();
    // p.Name = "华天晓";
    // p.Age = 15;
    // if (p.IsStudent)
    // {
    //   Student s = p;
    //   System.Console.WriteLine(s.Grade);
    // }
}

static void M02()
{
    var xml = File.ReadAllText("1.xml");
    var doc = XDocument.Parse(xml);
    var www = doc.Root?.Element("desc")?.Value;
    File.WriteAllText("111.txt", www);
    System.Console.WriteLine(www);
}

static void M03()
{
    // var aa = new List<string> { "24345", "6457567", "222", "222", "7676585678", "543523421" };
    // var bb = new HashSet<string>(aa);

    var id = Guid.NewGuid();
    var aa = new List<Guid> { Guid.NewGuid(), Guid.NewGuid(), id, id };
    var bb = new HashSet<Guid>(aa) { id };

    System.Console.WriteLine("{0}, {1}", aa.Count, bb.Count);
}

static void M04()
{
    var value = 5060312396537546206L;
    var keys2 = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; //编码,可加一
    var exponent2 = keys2.Length; //幂数
    string result = string.Empty;
    do
    {
        long index = value % exponent2;
        result = keys2[(int)index] + result;
        value = (value - index) / exponent2;
    } while (value > 0);

    System.Console.WriteLine('E' + result);
}

static void M05()
{
    var value = 66645381811439206;
    var keys2 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; //编码,可加一
    var exponent2 = keys2.Length; //幂数
    string result = string.Empty;
    do
    {
        long index = value % exponent2;
        result = keys2[(int)index] + result;
        value = (value - index) / exponent2;
    } while (value > 0);

    System.Console.WriteLine('M' + result);
}

static async void M06()
{
    var url = "http://localhost:5300/api/Thirdpart";
    var secret = "gc104uznyfs00164k52y296680";
    var request = new ThirdpartRequest
    {
        AppId = "7cdzd7g5d5w00",
        MD5 = "",
        ServerName = "getOrders",
        Body = "{\"ShopId\":\"f1558933-73e5-4375-b66c-f41065edb2b2\",\"Date\":\"2024-12-01\"}"
    };
    var content = request.AppId + request.ServerName + request.Body + secret;
    System.Console.WriteLine(content);
    request.MD5 = ParseMD5(content);
    var body = new StringContent(System.Text.Json.JsonSerializer.Serialize(request), Encoding.UTF8);
    body.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
    try
    {
        using var http = new HttpClient();
        var response = await http.PostAsync(url, body);
        var message = await response.Content.ReadAsStringAsync();
        System.Console.WriteLine(message);
    }
    catch (Exception e)
    {
        System.Console.WriteLine(e.ToString());
    }
}

static string ParseMD5(string input, Encoding? encoding = null)
{
    if (string.IsNullOrWhiteSpace(input))
        throw new ArgumentNullException(nameof(input));
    var bytResult = MD5.HashData((encoding ?? Encoding.UTF8).GetBytes(input));
    var strResult = BitConverter.ToString(bytResult);
    strResult = strResult.Replace("-", "");
    return strResult.ToLower();
}

static void M07()
{
    // var ww = "{\"name\":\"孙小双\",\"age\":22}";
    var ww = "分社如果为";
    var ww2 = JsonConvert.DeserializeObject<string>(ww);
    System.Console.WriteLine(ww);
    System.Console.WriteLine(ww2);
}

static void M08()
{
    var aaa = new MessageContentDto<MessageInfo>
    {
        SendTo = "111",
        QueryId = "222",
        Content = "333",
        PatientName = "444",
        PatientId = "555",
        VisitNo = "666",
        PushInfo = new MessageInfo
        {
            Mobile = "aaa",
            PatientName = "bbb",
            DetpName = "ccc",
            RefundBalance = "ddd",
            SucceedTime = "eee",
            Notes = "fff",
            Content = "ggg"
        }
    };
    // MessageContentDto bb = aaa;
    var www = my_method(aaa);

    string my_method<T>(T aa)
        where T : MessageContentDto
    {
        return aa.ToXml2();
    }
}

static void M09()
{
    var xml = File.ReadAllText("./2.xml");
    var json = xml.ToJObject();

    var status = json.GetNodeContent(
        "PRSC_IN010201UV01.controlActProcess.subject.actAppointment.statusCode.@code"
    );

    var codes = json.SelectToken(
            "PRSC_IN010201UV01.controlActProcess.subject.actAppointment.referencedOrder"
        )
        .ToList();
    var doctorAdviceNumber = codes
        .FirstOrDefault(
            a => a.SelectToken("actOrder.id.@root")?.ToString() == "2.16.156.10011.1.24"
        )
        ?.SelectToken("actOrder.id.@extension")
        ?.ToString();
    var hisOrderCode = codes
        .FirstOrDefault(
            a => a.SelectToken("actOrder.id.@root")?.ToString() == "2.16.156.10011.1.28"
        )
        ?.SelectToken("actOrder.id.@extension")
        ?.ToString();
    var patientCode = json.GetNodeContent(
        "PRSC_IN010201UV01.controlActProcess.subject.actAppointment.subject.patient.id.item",
        "@root|2.16.156.10011.2.5.1.4|@extension"
    );
    System.Console.WriteLine("状态：" + status);
    System.Console.WriteLine("医嘱号：" + doctorAdviceNumber);
    System.Console.WriteLine("申请单：" + hisOrderCode);
    System.Console.WriteLine("患者编码：" + patientCode);
}

static void M10()
{
    var buffer = File.ReadAllBytes(
        @"E:\ProgramData\Weixin\WeChat Files\sunxsalyr\FileStorage\File\2025-05\CRPDF.pdf"
    );
    var key = Encoding.UTF8.GetBytes("fe4914299cf044bf");

    System.Console.WriteLine("key长度：" + key.Length);
    var text = SM4Helper.EncryptECB(key, buffer);
    var base64 = Convert.ToBase64String(text);
    System.Console.WriteLine(base64);
    File.WriteAllText(
        @"E:\ProgramData\Weixin\WeChat Files\sunxsalyr\FileStorage\File\2025-05\CRPDF.txt",
        base64
    );
}
